/*
 * Подстроки
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>

struct suf_array {
	int sa;
	int32_t k1, k2;
};

/* {{{ memory allocation wrappers */
extern char *
s_calloc(size_t n, size_t size)
{
	void *ptr;
#ifdef MEMDEBUG
	fprintf(stderr, "%s(): calloc %lu times %lu bytes (tot %.4f MiB)\n",
			__func__, n, size, ((float) n * size) / (1024.0f * 1024.0f));
#endif /* MEMDEBUG */
	ptr = calloc(n, size);
	if (ptr == NULL) {
		fprintf(stderr, "%s(): out of memory, failed to allocate %lu bytes\n",
				__func__, n * size);
		abort();
	}
	return (char *) ptr;
}

extern char *
s_malloc(size_t size)
{
	void *ptr;
#ifdef MEMDEBUG
	fprintf(stderr, "%s(): malloc %lu bytes (%.4f MiB)\n",
			__func__, size, (float) size / (1024.0f * 1024.0f));
#endif /* MEMDEBUG */
	ptr = malloc(size);
	if (ptr == NULL) {
		fprintf(stderr, "%s(): out of memory, failed to allocate %lu bytes\n",
				__func__, size);
		abort();
	}
	return (char *) ptr;
}

extern char *
s_realloc(char *oldptr, size_t newsize)
{
	void *newptr;
#ifdef MEMDEBUG
	fprintf(stderr, "%s(): realloc oldptr %p for new size of %lu (%.4f MiB)\n",
			__func__, oldptr, newsize, (float) newsize / (1024.0f * 1024.0f));
#endif
	newptr = realloc(oldptr, newsize);
	if (newptr == NULL) {
		fprintf(stderr, "%s(): out of memory, "
				"failed to realloc %p for new size of %lu bytes\n",
				__func__, oldptr, newsize);
		abort();
	}
#ifdef MEMDEBUG
	fprintf(stderr, "%s(): newptr %p\n", __func__, newptr);
#endif /* MEMDEBUG */
	return (char *) newptr;
}

void
s_free(void *ptr)
{
#ifdef MEMDEBUG
	fprintf(stderr, "%s(): freeing %p\n", __func__, ptr);
#endif
	free(ptr);
}
/* }}} */

int
cmp_sa(const void *p1, const void *p2)
{
	const struct suf_array *s1 = (struct suf_array *) p1, *s2 = (struct suf_array *) p2;

	assert(s1 != NULL);
	assert(s2 != NULL);

	if (s1->k1 < s2->k1)
		return -1;
	else if (s1->k1 > s2->k1)
		return 1;
	else if (s1->k2 < s2->k2)
		return -1;
	else if (s1->k2 > s2->k2)
		return 1;

	return 0;
}

void
suf_array__print(const struct suf_array *sa, const int n)
{
	int i;
	for (i = 0; i < n; i++) {
		fprintf(stderr, "  { sa = %2d, key = (%2d, %2d) } [%d]\n",
				sa[i].sa, sa[i].k1, sa[i].k2, i);
	}
}

void
suf_array__sort_iter(const struct suf_array *sa, struct suf_array *result,
		const int n, const int shift)
{
#define RSORTKEY(i) (((((int64_t) 1LL) + sa[i].k1) << 32) | ((int64_t) (sa[i].k2) + 1LL))
#define INDEXVAL ((RSORTKEY(i) >> (shift << 2)) & 0xF)
	static int index[0x10];
	register int i, t1, t2;

	memset(index, 0, sizeof index);
	for (i = 0; i < n; i++)
		index[INDEXVAL]++;

	for (t1 = t2 = i = 0; i < 0x10; i++) {
		t1 = index[i];
		index[i] = t2;
		t2 += t1;
	}
	for (i = 0; i < n; i++)
		result[index[INDEXVAL]++] = sa[i];
#undef INDEXVAL
#undef RSORTKEY
}

int
suf_array__sort(struct suf_array *sa, const int n)
{
	struct suf_array *buf_sorted;
	int shift;
	buf_sorted = (struct suf_array *) s_malloc(n * sizeof *buf_sorted);
	for (shift = 0; shift < 16; shift++) {
		suf_array__sort_iter(sa, buf_sorted, n, shift);
		memcpy(sa, buf_sorted, n * sizeof *sa);
	}
	free(buf_sorted);
	return 0;
}

struct suf_array *
suf_array__build(const char *string, const int n)
{
	struct suf_array *A;
	int i, h = 1;
	int *isa, rank = 0;

	A = (struct suf_array *) s_calloc(n, sizeof *A);
	isa = (int *) s_calloc(n, sizeof *isa);

	for (i = 0; i < n; i++) {
		A[i].sa = i;
		A[i].k1 = 0;
		A[i].k2 = string[i];
	}

	//suf_array__print(A, n);
	while (rank != n - 1) {
		//fputc('\n', stderr);
		suf_array__sort(A, n);
		//suf_array__print(A, n);

		isa[0] = rank = 0;
		for (i = 1; i < n; i++) {
			if (cmp_sa(A + i - 1, A + i))
				rank++;
			isa[A[i].sa] = rank;
		}

		if (rank == n - 1)
			break; /* XXX */

		for (i = 0; i < n; i++) {
			A[i].k1 = isa[A[i].sa];
			if (A[i].sa + h >= n)
				A[i].k2 = -1;
			else
				A[i].k2 = isa[A[i].sa + h];
		}

		h *= 2;
	}

	free(isa);
	return A;
}

int
suf_array__count_substr(struct suf_array *sa, const int n, const char *str,
		const char *substr)
{
	int l, r, m;
	int start;
	size_t substr_len = strlen(substr);

	l = 0, r = n;
	while (l < r) {
		m = (r + l) / 2;
		if (strncmp(substr, str + sa[m].sa, substr_len) > 0)
			l = m + 1;
		else
			r = m;
	}
	start = l;

	l = 0, r = n;
	while (l < r) {
		m = (r + l) / 2;
		if (strncmp(substr, str + sa[m].sa, substr_len) >= 0)
			l = m + 1;
		else
			r = m;
	}

	return l - start;
}

int
main(int argc, char **argv)
{
	char *string;
	char *substr;
	struct suf_array *A;
	int i, n, len;

	string = s_malloc(50002UL);
	scanf("%s", string);
	A = suf_array__build(string, len = strlen(string));

	substr = s_malloc(1000002UL);
	scanf("%d\n", &n);
	for (i = 0; i < n; i++) {
		scanf("%s", substr);
		printf("%d ", suf_array__count_substr(A, len, string, substr));
	}
	fputc('\n', stdout);

	free(substr);
	free(string);
	free(A);
	return 0;
}
